using InterfaceWeaver.AopCore;
using Roslyn.Compilers.CSharp;

namespace InterfaceWeaver.CustomTool
{
    public class InheritAspectHandler : AspectHandlerBase<InheritAttribute>
    {
        /// <summary>
        /// Handles the on interface.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="attributeSyntax">The attribute syntax.</param>
        /// <param name="interfaceDeclarationSyntax">The interface declaration syntax.</param>
        /// <returns>
        /// True if the aspect is handled, false otherwise
        /// </returns>
        public override void HandleOnInterface(AspectDispatcher context, AttributeSyntax attributeSyntax, InterfaceDeclarationSyntax interfaceDeclarationSyntax)
        {
            var currentType = context.WeavedCodeContext.CurrentTypeDeclaration;
            var type = attributeSyntax.GetParameterTypeOf();
            var baseType = type.ToTypeReference();
            if (currentType.BaseTypes.Count == 0)
            {
                currentType.BaseTypes.Add(baseType);
            }
            else
            {
                currentType.BaseTypes.Insert(0, baseType);
            }
        }
    }
}